<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>后台管理 - 电影网站</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/style.css">
    <script src="/js/echarts.min.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <div class="container-fluid">
        <a class="navbar-brand" href="/admin">后台管理系统</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a class="nav-link active" href="/admin/movies">电影管理</a>
                    <button class="btn btn-success" onclick="window.location.href='/admin/export/ranking'">导出排行榜</button>
                    
                </li>
            </ul>
            <ul class="navbar-nav ms-auto">
                <li class="nav-item">
                    <a class="nav-link" href="/">返回前台</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/logout">退出登录</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<div class="container-fluid mt-4">
    <div class="row">
        <div class="col-md-6 mb-4">
            <div class="card h-100">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <span>播放量统计</span>
                    <div class="btn-group btn-group-sm">
                        <button class="btn btn-outline-secondary active" onclick="updatePlayCountChart('weekly')">周</button>
                        <button class="btn btn-outline-secondary" onclick="updatePlayCountChart('monthly')">月</button>
                        <button class="btn btn-outline-secondary" onclick="updatePlayCountChart('yearly')">年</button>
                    </div>
                </div>
                <div class="card-body">
                    <div id="playCountChart" style="height: 300px;"></div>
                </div>
            </div>
        </div>
        <div class="col-md-6 mb-4">
            <div class="card h-100">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <span>VIP订单统计</span>
                    <div class="btn-group btn-group-sm">
                        <button class="btn btn-outline-secondary active" onclick="updateOrderChart('weekly')">周</button>
                        <button class="btn btn-outline-secondary" onclick="updateOrderChart('monthly')">月</button>
                        <button class="btn btn-outline-secondary" onclick="updateOrderChart('yearly')">年</button>
                    </div>
                </div>
                <div class="card-body">
                    <div id="orderChart" style="height: 300px;"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <span>数据报表</span>
                    <a href="/admin/export/ranking" class="btn btn-primary btn-sm">导出Excel</a>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>排名</th>
                                    <th>电影名称</th>
                                    <th>分类</th>
                                    <th>评分</th>
                                    <th>播放次数</th>
                                    <th>上映时间</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr th:each="movie, stat : ${movies}">
                                    <td th:text="${stat.count}"></td>
                                    <td th:text="${movie.name}"></td>
                                    <td th:each="category : ${movie.categories}" th:text="${category} "></td>
                                    <td th:text="${movie.score}"></td>
                                    <td th:text="${movie.playCount}"></td>
                                    <td th:text="${#dates.format(movie.releaseDate, 'yyyy-MM-dd')}"></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.bundle.min.js"></script>
<script>
// 初始化ECharts实例
var playCountChart = echarts.init(document.getElementById('playCountChart'));
var orderChart = echarts.init(document.getElementById('orderChart'));

// 播放量统计图表配置
function updatePlayCountChart(type) {
    $.get('/admin/stats/playcount?type=' + type, function(data) {
        playCountChart.setOption({
            title: {
                text: '播放量统计'
            },
            tooltip: {
                trigger: 'axis'
            },
            xAxis: {
                type: 'category',
                data: data.dates
            },
            yAxis: {
                type: 'value'
            },
            series: [{
                name: '播放量',
                type: 'line',
                data: data.counts,
                smooth: true,
                areaStyle: {}
            }]
        });
    });
}

// VIP订单统计图表配置
function updateOrderChart(type) {
    $.get('/admin/stats/orders?type=' + type, function(data) {
        orderChart.setOption({
            title: {
                text: 'VIP订单统计'
            },
            tooltip: {
                trigger: 'axis'
            },
            xAxis: {
                type: 'category',
                data: data.dates
            },
            yAxis: {
                type: 'value'
            },
            series: [{
                name: '订单数',
                data: data.counts,
                itemStyle: 
                    '#91cc75'
                
            }]
        })
    });
}

// 初始化加载
updatePlayCountChart('weekly');
updateOrderChart('weekly');

// 窗口大小改变时自动调整图表大小
window.addEventListener('resize', function() {
    playCountChart.resize();
    orderChart.resize();
});
</script>
</body>
</html>